<template>
  <div class="container">
    <div class="header">
      <div class="title">简历管理</div>
    </div>
    <div class="main">
      <Form ref="formRef" class="form-box" />
    </div>
    <div class="footer">
      <el-button type="primary" @click="update">更新简历</el-button>
    </div>
  </div>
</template>

<script setup>
import to from 'await-to-js'
import Api from '@/api'
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import Form from './components/Form.vue'

import { useUserStore } from '@/store/modules/user'

const userStore = useUserStore()

const formRef = ref()

const resumeId = ref('')

// 获取用户简历详情
const getResumeDetail = async () => {
  const params = {
    user_id: userStore.userInfo.user_id
  }

  console.log('%c [ getResumeDetail ]-36', 'font-size:13px; background:pink; color:#bf2c9f;', params)
  const [err, res] = await to(Api.resume.getResumeDetail(params))
  if (!err) {
    const resData = res.data
    console.log('%c [ getResumeDetail ]-23', 'font-size:13px; background:pink; color:#bf2c9f;', res)
    resumeId.value = resData ? resData._id : ''
    formRef.value.resumeForm = resData || formRef.value.resumeForm
    formRef.value.job_tags = resData.job_tags || formRef.value.job_tags
  }
}

const update = async () => {
  const formData = formRef.value.resumeForm
  formData.job_tags = formRef.value.job_tags
  console.log('%c [ update - before ]-40', 'font-size:13px; background:pink; color:#bf2c9f;', formData)
  const req = resumeId.value ? Api.resume.updateResume : Api.resume.addResume
  const params = resumeId.value
    ? {
        _id: resumeId.value,
        resume_data: formData
      }
    : {
        user_id: userStore.userInfo.user_id,
        resume_data: formData
      }
  const [err, res] = await to(req(params))
  if (!err) {
    ElMessage({
      message: '简历更新成功',
      type: 'success'
    })
    getResumeDetail()
    console.log('%c [ update - success ]-40', 'font-size:13px; background:pink; color:#bf2c9f;', res)
  }
}

onMounted(() => {
  getResumeDetail()
})
</script>

<style lang="scss" scoped>
$footerHeight: 80px;
.container {
  position: relative;
  width: 100%;
  height: 100%;
  padding: 40px 30px $footerHeight;
  display: flex;
  flex-direction: column;
  color: #666666;
  :deep(.tbl-header) {
    height: 70px;
  }
  :deep(.tbl-row) {
    height: 60px;
  }
  .header {
    margin-bottom: 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .title {
      font-size: 24px;
    }
  }
  .main {
    flex: 1;
    overflow: auto;
    .form-box {
      width: 80%;
    }
  }
  .footer {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: $footerHeight;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
